package cn.ante.org.controller;

import cn.ante.basic.annotation.PreAuthorize;
import cn.ante.basic.utils.AjaxResult;
import cn.ante.basic.utils.PageList;
import cn.ante.org.domain.Employee;
import cn.ante.org.query.EmployeeQuery;
import cn.ante.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "员工的api",description="员工相关的crud功能")
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService service;

    /*
    * @Title: loadAll
    * @Description: 分页查询所有
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/6/24 11:49
    * @Parameters: [employeeQuery]
    * @Return cn.ante.basic.utils.PageList<cn.ante.org.domain.Employee>
    */
    @PostMapping
    @ApiOperation(value = "分页员工列表")
    @PreAuthorize(name ="员工列表分页展示",sn="employee:list")
    public PageList<Employee> findAll(@RequestBody EmployeeQuery employeeQuery){

        return service.findAll(employeeQuery);

    }

    /*
    * @Title: loadById
    * @Description: 根据id查询员工信息
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/6/24 11:51
    * @Parameters: [id]
    * @Return cn.ante.org.domain.Employee
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询员工信息")
    @PreAuthorize(name ="id查询员工信息",sn="employee:get")
    public Employee loadById(@ApiParam(name="主键",required = true) @PathVariable("id") Long id){
        return service.loadById(id);
    }

    /**
    * @Description: 根据id删除
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/7/15 13:04
    */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除员工信息")
    @PreAuthorize(name ="根据id删除员工",sn="employee:delete")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            service.remove(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙！！");
        }
    }

    /**
    * @Description: 保存/修改员工信息
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/7/15 13:05
    */
    @PutMapping
    @ApiOperation(value = "保存/修改员工信息")
    @PreAuthorize(name ="保存/修改员工信息",sn="employee:addOrUpt")
    public AjaxResult addOrUpt(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                service.add(employee);
                System.out.println(employee);
            }else{
                service.update(employee);
            }
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙！！");
        }
    }

    /**
    * @Description: 批量删除员工信息
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/7/15 13:07
    */
    @PatchMapping
    @ApiOperation(value = "批量删除员工信息")
    @PreAuthorize(name ="批量删除员工信息",sn="employee:batchDel")
    public AjaxResult batchDel( @RequestBody Long[] ids){
        try {
            service.batchDel(ids);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙！！");
        }
    }

    /**
    * @Description: 员工列表查询
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/7/15 13:07
    */
    @GetMapping
    @ApiOperation(value = "查询员工列表")
    public List<Employee> loadAll(){
        List<Employee> employeePageList = service.loadAll();
        return employeePageList;
    }

}
